<script setup>
import { ref, onMounted } from 'vue'
import { getUserInfo } from '@/api/user'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import Header from '../components/Header.vue'

const user = ref(null)
const router = useRouter()

onMounted(async () => {
  try {
    const res = await getUserInfo()
    if (res.code === 200) {
      user.value = res.data
    } else {
      ElMessage.error(res.message || '获取用户信息失败')
    }
  } catch (e) {
    ElMessage.error('获取用户信息失败')
  }
})

const goToUpdate = () => {
  window.scrollTo(0, 0);
  router.push('/user/update');
}
</script>

<template>
  <div class="user-profile-container">
    <Header />
    <div class="profile-background">
      <div class="profile-card" v-if="user">
        <div class="profile-header">
          <el-avatar :src="user.avatar" :size="100" class="user-avatar" />
          <h2 class="user-name">{{ user.nickname || user.username }}</h2>
          <p class="user-email">{{ user.email }}</p>
        </div>
        
        <div class="profile-content">
          <h3 class="section-title">用户信息</h3>
          
          <div class="info-grid">
            <div class="info-item">
              <div class="info-label">用户名</div>
              <div class="info-value">{{ user.username }}</div>
            </div>
            
            <div class="info-item">
              <div class="info-label">昵称</div>
              <div class="info-value">{{ user.nickname || '-' }}</div>
            </div>
            
            <div class="info-item">
              <div class="info-label">邮箱</div>
              <div class="info-value">{{ user.email || '-' }}</div>
            </div>
            
            <div class="info-item">
              <div class="info-label">手机号</div>
              <div class="info-value">{{ user.phone || '-' }}</div>
            </div>
            
            <div class="info-item">
              <div class="info-label">状态</div>
              <div class="info-value">
                <el-tag :type="user.status === 1 ? 'success' : 'danger'" effect="light">
                  {{ user.status === 1 ? '正常' : '禁用' }}
                </el-tag>
              </div>
            </div>
            
            <div class="info-item">
              <div class="info-label">角色</div>
              <div class="info-value">
                <el-tag 
                  v-for="role in user.roles || []" 
                  :key="role" 
                  class="role-tag" 
                  :color="role.includes('admin') ? '#e6f7ff' : '#f0f9eb'"
                  :effect="role.includes('admin') ? 'dark' : 'plain'"
                >{{ role }}</el-tag>
              </div>
            </div>
          </div>
        </div>
        
        <div class="profile-footer">
          <el-button type="primary" class="update-btn" @click="goToUpdate">
            更新个人信息
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>


<style scoped>
.user-profile-container {
  min-height: 100vh;
  font-family: 'Inter', 'Source Sans Pro', 'Microsoft YaHei', sans-serif;
}

.profile-background {
  background: linear-gradient(135deg, #f5f7fa 0%, #e4ebf5 100%);
  min-height: calc(100vh - 60px);
  padding: 50px 20px;
}

.profile-card {
  max-width: 700px;
  margin: 0 auto;
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.profile-header {
  background: linear-gradient(135deg, #2c5dff 0%, #0097ff 100%);
  color: white;
  padding: 40px 0 30px;
  text-align: center;
  position: relative;
}

.profile-header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 30px;
  background: white;
  border-radius: 30px 30px 0 0;
}

.user-avatar {
  border: 5px solid rgba(255, 255, 255, 0.7);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
  margin-bottom: 15px;
}

.user-name {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 5px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.user-email {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
}

.profile-content {
  padding: 20px 40px 40px;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 20px;
  position: relative;
  padding-left: 15px;
}

.section-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 20px;
  background: linear-gradient(180deg, #2c5dff 0%, #0097ff 100%);
  border-radius: 2px;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 25px 40px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.info-label {
  font-size: 14px;
  color: #6b7280;
}

.info-value {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.role-tag {
  margin-right: 6px;
  margin-bottom: 6px;
}

.profile-footer {
  padding: 0 40px 30px;
}

.update-btn {
  width: 100%;
  height: 46px;
  background: linear-gradient(135deg, #2c5dff 0%, #0097ff 100%);
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  transition: all 0.3s;
}

.update-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(44, 93, 255, 0.25);
}

@media (max-width: 768px) {
  .info-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .profile-content {
    padding: 20px 25px 30px;
  }
  
  .profile-footer {
    padding: 0 25px 25px;
  }
}
</style> 